
const mybehavior = require("../../../../../behaviors/requests")
import WxValidate from "../../../../../Wx_Validate/WxValidate"
import Toast from '../../../../../miniprogram_npm/@vant/weapp/toast/toast';
// const app = getApp()
const options11 = {
  K1: ['8号电梯口', '12号电梯口',],
  K2: ['30号电梯口', '45号电梯口', '定额库'],
  K3: ['96号电梯口',],
  K4: ['东南门', '九期东南角', '水箱4号电机'],
  SZ: ['104号电梯口',],
};
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    base_urlstr: "https://logisticsengineering.gree.com/order_system",
    employee_num: "",
    employee_name: "",
    task_quantity: 1,
    starting_point: "",
    end_point: "",
    popup_show0: false,
    popup_show1: false,
    options0: ["15号电梯", "18号电梯",],

    options1: [
      {
        values: Object.keys(options11),
      },
      {
        values: options11['K1'],
      },
    ],
  },

  // 点击--配送起点--输入框
  onClick0(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show0: true
    })
  },
  // 选择--配送起点--输入框关闭
  onClose0() {
    this.setData({ popup_show0: false });
  },
  // 确定-配送起点--弹出框关闭
  onChange0(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      starting_point: value,
      popup_show0: false,
    })
  },
  // 数量按钮
  onStepperChange(value) {
    this.setData({
      task_quantity: value.detail
    });
  },


  // 点击--配送终点--输入框
  onClick1(event) {
    // event.detail 为当前输入的值
    // console.log(event.detail);
    this.setData({
      popup_show1: true
    })
  },
  // 选择--配送终点--输入框关闭
  onClose1() {
    this.setData({ popup_show1: false });
  },
  onChange1(event) {
    console.log("111", event);
    const { picker, value, index } = event.detail;
    picker.setColumnValues(1, options11[value[0]]);
  },
  // 确定-配送终点--弹出框关闭
  onConfirm1(event) {
    const { picker, value, index } = event.detail;
    let val = event.detail.value[0] + "-" + event.detail.value[1]
    this.setData({
      end_point: val,
      popup_show1: false,
    })
  },


  // 点击提交按钮
  formSubmit(e) {
    let that = this
    let url = that.data.base_urlstr + "/add_transfer_request";
    let data = {
      employee_num: this.data.employee_num,
      employee_name: this.data.employee_name,
      starting_point: this.data.starting_point,
      end_point: this.data.end_point,
      task_quantity: this.data.task_quantity,
    }
    console.log("123123", data);
    if (!that.WxValidate.checkForm(data)) {
      const error = that.WxValidate.errorList[0]
      Toast(error.msg)
    } else {
      that.post(url, data).then(res => {
        console.log("返回结果", res)
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          this.formReset()
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })
    }



    // console.log(data)
  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      employee_name: {
        required: true,
      },
      starting_point: {
        required: true,
      },
      end_point: {
        required: true,
      },
      task_quantity: {
        required: true,
      },

    }

    let message = {
      employee_name: {
        required: "创建人不能为空",
      },
      starting_point: {
        required: "配送起点不能为空",
      },
      end_point: {
        required: "配送终点不能为空",
      },
      task_quantity: {
        required: "数量不能为空",
      },

    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 点击重置按钮
  formReset() {
    this.setData({
      starting_point: "",
      end_point: "",
      task_quantity: 1,
 
    })
  },



  // 获取数据
  getDataList() {
    let val = wx.getStorageSync('_id')
    let self = this;
    let url = self.data.base_urlstr + "/get_delivery_starting_point"
    let data = {
      employee_num: val,

    }
    this.post(url, data).then(res => {
      if (res.code == 20000) {
        console.log(res);
        // let show_list = res.data.results
        // self.setData({
        //   data_arr: show_list[0],
        // })

      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })

      }
    }).catch(err => {

      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getDataList()
    this.setData({
      employee_num: wx.getStorageSync('_id'),

      employee_name: wx.getStorageSync('name')
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})